package com.gmall.rabbitmq.producer.controller;

import com.gmall.rabbitmq.config.RabbitMQConfig_Delay2;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("delay2")
public class Delay2Controller {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @PostMapping("")
    public String producerMessage(){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rabbitTemplate.convertAndSend(RabbitMQConfig_Delay2.Exchange_Custom_Delay,
                RabbitMQConfig_Delay2.ROUTING_KEY_1,
                simpleDateFormat.format(new Date()),
                (message)->{
                    //设置延迟时间
                    message.getMessageProperties().setMessageId("987654321");
                    message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    message.getMessageProperties().setDelay(1000*10);
                    return message;
                });

        return "OK";
    }
}
